<?php
namespace App\Components\Admin;

use App\Components\Twig\Twig;
use Psr\Http\Message\ResponseInterface;

class ResponseHelper
{
    public static function createTwig()
    {
        return Twig::create(ROOT_PATH . 'template', [
            'debug' => $_ENV['app_debug'],
            'cache' => ROOT_PATH . 'storage/view',
        ]);
    }

    public static function success(ResponseInterface $response, array $data = [], int $code = 0)
    {
        $body = ['code' => $code, 'data' => $data];
        $response->getBody()->write(json_encode($body));
        return $response->withHeader('Content-Type', 'application/json');
    }

    public static function error(ResponseInterface $response, string $msg, int $code = 1)
    {
        $body = ['code' => $code, 'msg' => $msg];
        $response->getBody()->write(json_encode($body));
        return $response->withHeader('Content-Type', 'application/json');
    }
}
